﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CodeRendere
{
    public partial class Form1 : Form
    {
        public static readonly string LINE_FORMAT = "public static readonly int {0} = {1};";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Code Files|*.txt";
            open.DefaultExt = "txt";

            DialogResult results = open.ShowDialog();
            if (results == DialogResult.OK)
            {
                string filename = open.FileName;
                this._RenderFile(filename);
            }
        }

        private void _RenderFile(string filename)
        {
            if (!File.Exists(filename))
            {
                MessageBox.Show(this, "File " + filename + " was not accessible.", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List<string> items = new List<string>();
            try
            {
                string[] lines = File.ReadAllLines(filename);
                foreach (string line in lines)
                {
                    char[] delimeters = { ' ', '\t' };
                    string[] pop = line.Split(delimeters);
                    items.Add(string.Format(LINE_FORMAT, pop[0], pop[1]));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, "Error while reading file.\n\n" + e.Message, "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.textBox1.Text = string.Empty;
            foreach (string item in items)
            {
                this.textBox1.Text += item + Environment.NewLine;
            }
        }
    }
}
